const Emitter = require('events');

class EnvContext {
    static getInstance() {
        if (!EnvContext.instance) {
            EnvContext.instance = new EnvContext()
        }
        return EnvContext.instance
    }

    setMainWin(mainWin) {
        this.mainWin = mainWin
    }

    getMainWin() {
        return this.mainWin
    }

    setApi(api) {
        this.api = api
    }


    setPanelWin(panel) {
        this.panel = panel
    }

    getPanelWin() {
        return this.panel
    }

    setLastScreanId(screanId) {
        this.lastScreanId = screanId
    }

    getLastScreanId() {
        return this.lastScreanId
    }

    setBrowserWindow(pluginId, win) {
        if (this.browserWindows == null) {
            this.browserWindows = {}
        }
        this.browserWindows[pluginId] = win
    }

    getBrowserWindow(pluginId) {
        if (this.browserWindows == null) {
            return null
        }
        return this.browserWindows[pluginId]
    }

    getEmitter() {
        if (this.emitter == undefined) {
            this.emitter = new Emitter()
        }
        return this.emitter
    }


    getViewByPluginId(pluginId) {
        if (this.views == undefined) {
            this.views = {}
            return null
        } else {
            return this.views[pluginId]
        }
    }

    setViewByPluginId(pluginId, view) {
        if (this.views == undefined) {
            this.views = {}
        }
        this.views[pluginId] = view
    }

    /*
    * 系统托盘展开/收缩状态
    */
    getTrayStatus() {
        if (this.trayStatus == undefined) {
            this.trayStatus = false
            return false
        } else {
            return this.trayStatus
        }
    }

    /*
    * 系统托盘展开/收缩状态
    */
    setTrayStatus(status) {
        this.trayStatus = status
    }


    /*
    * 根据pluginId获取托盘
    */
    getTray(pluginId) {
        if (this.trays == undefined) {
            this.trays = {}
            return null
        } else {
            return this.trays[pluginId]
        }
    }

    /*
    * 获取所有托盘应用
    */
    getTrays() {
        if (this.trays == undefined) {
            this.trays = {}
            return {}
        } else {
            return this.trays
        }
    }

    /*
    * 设置托盘应用
    */
    setTray(pluginId, tray) {
        this.trays[pluginId] = tray
    }

    /*
    * 设置托盘应用主窗口
    */
    setTrayWindow(pluginId, win) {
        if (this.trayWindows == null) {
            this.trayWindows = {}
        }
        this.trayWindows[pluginId] = win
    }

    /*
    * 获取托盘应用主窗口
    */
    getTrayWindow(pluginId) {
        if (this.trayWindows == null) {
            return null
        }
        return this.trayWindows[pluginId]
    }

    /*
    * 获取所有托盘应用主窗口
    */
    getTrayWindows() {
        if (this.trayWindows == null) {
            this.trayWindows = {}
        }
        return this.trayWindows
    }

}

module.exports = EnvContext;
